<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <style>
         #box ul{
            list-style: none;
            width: 60%;
            margin: auto;
            border: 1px solid black;
            padding: 10px;
        }
        #box ul li{
            display: flex;
            justify-content: space-around;
            margin-top: 10px;
            border-bottom: 1px dashed #cccccc;
        }
        #box ul li:last-child{
            border-bottom: none;
        }
        #box ul li img{
            width: 100px;
        }
        .all{
            width: 60%;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="box">
        <div class="all" v-if="list.length === 0">购物车空空如也</div>
        <template v-else>
            <div class="all" >
                <input type="checkbox" @change="selectAllGood" v-model="isAll">全选|全不选
            </div>
            <ul>
                <li v-for="(item,index) in list" :key="item.id">
                    <div>
                        <input type="checkbox" v-model="checkGroup" :value="item" @change="selectOne">
                    </div>
                    <div>
                        <img :src="item.pic" alt="">
                    </div>
                    <div>
                        <div>
                            名称：{{ item.name }}
                        </div>
                        <div>
                            价格：{{ item.price }}
                        </div>
                    </div>
                    
                    <div>
                        <button @click="item.number--" :disabled="item.number === 1 ? true : false">-</button>
                        {{ item.number }}
                        <button @click="item.number++" :disabled="item.number === item.limit ? true : false">+</button>
                    </div>
                    <div>
                        <button @click="delClick(item.id)">删除</button>
                    </div>
                </li>
            </ul>
            <div class="all" >总金额: {{ getTotel() }} </div>
        </template>  
    </div>
    <script>
        let app = new Vue({
            el: "#box",
            data: {
                checkGroup:[],
                isAll:false,
                list:[
                    {
                        name:"商品1",
                        price:10,
                        number:1,
                        id:1,
                        limit:5,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品2",
                        price:20,
                        number:5,
                        id:2,
                        limit:8,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品3",
                        price:30,
                        number:10,
                        id:3,
                        limit:15,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品4",
                        price:8,
                        number:6,
                        id:4,
                        limit:18,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品5",
                        price:14,
                        number:2,
                        id:5,
                        limit:7,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    }
                ]
            },
            methods:{
                selectAllGood(){
                    if(this.isAll){
                        this.checkGroup = this.list
                    }
                    else{
                        this.checkGroup = []
                    }
                },
                selectOne(){
                    if(this.checkGroup.length === this.list.length){
                        this.isAll = true
                    }
                    else{
                        this.isAll = false
                    }
                },
                getTotel(){
                    var sum = 0
                    this.checkGroup.forEach((element) => {sum += element.number * element.price})
                    return sum
                },
                delClick(sign){                  
                   this.list = this.list.filter((element) => element.id != sign)
                   this.checkGroup = this.checkGroup.filter((element) => element.id != sign)
                   this.selectOne()
                }
            }
        })
    </script>
</body>
</html>